package sample;

import javafx.scene.media.Media;

import java.io.File;
import java.io.FileNotFoundException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class LoadSong {

    public List<String> loadSong(String path, List<Media> list) throws FileNotFoundException, URISyntaxException, MalformedURLException {
//        FileReader fileReader = new FileReader("file:/home/hankun/Music/music");
//        getClass().getResource("file:/home/hankun/Music/music/无期.mp3");
//        File file =  new File("/home/hankun/Music/music/无期.mp3");
//
//        URI uri = new URI("file:///home/hankun/Music/music/无期.mp3");
//        URL url = uri.toURL();
//        String s = uri.t,oString();


        File file = new File(path);
        System.out.println(file.getName());

        return analyseDir(file,list);
    }

    public List<String> analyseDir(File dir,List<Media> list) throws URISyntaxException, MalformedURLException {
        List<String> stringList = new ArrayList<>();
        String head = "file://";
        if(dir.exists()){
            File[] files = dir.listFiles();
            for (int i = 0; i < files.length; i++) {
                if(files[i].isDirectory()){
                    analyseDir(files[i],list);
                }else{
                    stringList.add(files[i].getName());
                    String absolutePath = files[i].getAbsolutePath();
                    absolutePath = head+absolutePath;
                    URL url = new URI(absolutePath).toURL();
                    Media media = new Media(url.toExternalForm());
                    list.add(media);
                    System.out.println(files[i].getName());
                }
            }
        }
        return stringList;
    }
//
//    public static void main(String[] args) throws FileNotFoundException, MalformedURLException, URISyntaxException {
//        new LoadSong().loadSong("/home/hankun/project/demofx/music",new ArrayList<Media>());
//    }

}
